package com.lucene.scheduled;
import com.google.gson.JsonObject;
import com.lucene.handle.TulingMessageHandler;
import com.lucene.model.Environment;
import com.lucene.service.WechatService;
import com.lucene.utils.Utils;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by zhangzhen on 23/10/2017.
 */
@Component
public class TaskJob{
    private static Logger logger = Logger.getLogger(TaskJob.class);
    WechatService ws = WechatService.build(null);

    @Scheduled(cron="0/1 * *  * * ? ")   //每秒执行一次
    public void proMsg(){
        if (ws.getStatus()!=null){
            if (ws.getStatus().equals("wait4login")){//执行登录操作
                String result = ws.wait4login();
                if (!"200".equals(result)){
                    logger.info("[*] 微信登录失败. 代码：{"+result+"}\n");
                    ws.setStatus(null);
                }
            }else if (ws.getStatus().equals("waiting")){//等待扫码登录中

            }else if (ws.getStatus().equals("waitingok")){//登录成功
                if (ws.login()){
                    logger.info("[*] 微信登录成功.\n");
                    ws.setStatus("loginok");
                }else {
                    logger.info("[*] 微信登录失败.\n");
                    ws.setStatus(null);
                }
            }else if (ws.getStatus().equals("loginok")){
                if (ws.init()){
                    ws.setStatus("initok");
                    logger.info("[*] 微信初始化成功.\n");
                }else {
                    ws.setStatus(null);
                    logger.info("[*] 微信初始化失败.\n");
                }
            }else if (ws.getStatus().equals("initok")){
                if (ws.openStatusNotify()){
                    ws.setStatus("openok");
                    logger.info("[*] 微信打开通知成功.\n");
                }else {
                    ws.setStatus(null);
                    logger.info("[*] 微信打开通知成功失败.\n");
                }
            }else if (ws.getStatus().equals("openok")){
                if (ws.getContact()){
                    logger.info("[*] 获取 {"+ws.getContactList().size()+"} 联系人.\n");
                    logger.info("[*] 开始处理消息.\n");
                    ws.setStatus("listenning");
                }else {
                    ws.setStatus(null);
                    logger.info("[*] 微信获取联系人失败.\n");
                }
            }else if (ws.getStatus().equals("listenning")){
                try {
                    ws.setMessageHandle(new TulingMessageHandler(Environment.of("classpath:application.properties")));
                    if (ws.testSynCheck()){
                        ws.setStatus("loginsuccess");
                    }else {
                        ws.setStatus(null);
                    }
                }catch (IOException e){
                    ws.setStatus(null);
                    e.printStackTrace();
                }
            }else if (ws.getStatus().equals("loginsuccess")){
                try {
                    int[] checkResponse = ws.synCheck();
                    int retcode = checkResponse[0];
                    int selector = checkResponse[1];
                    logger.debug("[*] retcode: {"+retcode+"}, selector: {"+selector+"}");
                    if (retcode == 0){
                        if (selector == 0){

                        }else if (selector == 2){//新消息
                            JsonObject dic = ws.wxSync();
                            if (null != dic) {
                                ws.handle_msg(dic);
                            }
                        }else if (selector == 3){//未知消息
                            JsonObject dic = ws.wxSync();
                            if (null != dic) {
                                ws.handle_msg(dic);
                            }
                        }else if (selector == 4){//通讯录更新
                            JsonObject dic = ws.wxSync();
                            if (null != dic) {
                                ws.getContact();
                            }
                        }else if (selector == 6){//可能是红包
                            JsonObject dic = ws.wxSync();
                            if (null != dic) {
                                ws.handle_msg(dic);
                            }
                        }else if (selector == 7){//在手机上操作了微信
                            JsonObject dic = ws.wxSync();
                            if (null != dic) {
                                ws.handle_msg(dic);
                            }
                        }else {
                            logger.info("[*] synheck retcode: {"+retcode+"}, selector: {"+selector+"}");
                            JsonObject dic = ws.wxSync();
                            if (null != dic) {
                                ws.handle_msg(dic);
                            }
                        }
                    }else if (retcode == 1100){
                        logger.info("[*] 从微信客户端上登出了微信\n");
                        ws.setStatus(null);
                    }else if(retcode == 1101){
                        logger.info("[*] 你在其他地方登录了 WEB 版微信\n");
                        ws.setStatus(null);
                    }else if (retcode == 1102){
                        logger.info("[*] 你在手机上主动退出了\n");
                        ws.setStatus(null);
                    }else {
                        logger.info("[*] synCheck : {"+retcode+"}, selector: {"+selector+"}");
                    }
                } catch (Exception e){
                    logger.info("[*] Excepet in process Message\n");
                    e.printStackTrace();
                }
            }
        }
    }
}
